package com.ruicar.afs.cloud.apply.admin.auth.controller;

import com.ruicar.afs.cloud.apply.admin.auth.vo.AuthRegionVO;
import com.ruicar.afs.cloud.apply.admin.manage.entity.SysChannelAuthRegion;
import com.ruicar.afs.cloud.apply.admin.manage.entity.SysDeptAuthRegion;
import com.ruicar.afs.cloud.apply.admin.manage.service.ApplyChannelAuthRegionService;
import com.ruicar.afs.cloud.apply.admin.manage.service.ApplyUserService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.apply.dto.RegionDTO;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 渠道授权区域
 * @author: sijun.yu
 * @date: 2020/7/8 14:17
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/authRegion")
public class AuthRegionController {

    private final ApplyChannelAuthRegionService channelAuthRegionService;
    private final ApplyUserService userService;

    /**
     * 获取省份或城市
     *
     * @param regionDTO
     * @return
     */
    @ApiOperation("获取省份或城市")
    @PostMapping(value = "/getAddressList")
    public IResponse getChannelAddressList(@RequestBody RegionDTO regionDTO) {

        List<SysChannelAuthRegion> authRegionList = channelAuthRegionService.getChannelAuthRegionList(regionDTO.getChannelId(), regionDTO.getBusinessType(), regionDTO.getUpperCode());
        List<AuthRegionVO> resultList = new ArrayList<AuthRegionVO>();
        for (SysChannelAuthRegion channelAuthRegion : authRegionList) {
            AuthRegionVO vo = new AuthRegionVO();
            vo.setValue(channelAuthRegion.getCode());
            vo.setLabel(channelAuthRegion.getTitle());
            vo.setUpperCode(channelAuthRegion.getParentId());
            vo.setIsParent(channelAuthRegion.getIsParent());
            resultList.add(vo);
        }
        return IResponse.success(resultList);
    }

    /**
     * 获取部门下省份和城市
     *
     * @param regionDTO
     * @return
     */
    @ApiOperation("获取部门下省份和城市")
    @PostMapping(value = "/getDeptAddressList")
    public IResponse getDeptAddressList(@RequestBody RegionDTO regionDTO) {

        List<AuthRegionVO> resultList = new ArrayList<AuthRegionVO>();
        List<SysDeptAuthRegion> authRegionList = userService.getDeptAddressList(regionDTO.getUsername(), regionDTO.getBusinessType(), regionDTO.getUpperCode());
        for (SysDeptAuthRegion deptAuthRegion : authRegionList) {
            AuthRegionVO vo = new AuthRegionVO();
            vo.setValue(deptAuthRegion.getCode());
            vo.setLabel(deptAuthRegion.getTitle());
            vo.setUpperCode(deptAuthRegion.getParentId());
            vo.setIsParent(deptAuthRegion.getIsParent());
            resultList.add(vo);
        }
        return IResponse.success(resultList);
    }
}


